package com.atguigu.gulimall.product.controller;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.entity.ProductAttrValueEntity;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.ProductAttrValueService;
import com.atguigu.gulimall.product.vo.AttrResVo;
import com.atguigu.gulimall.product.vo.AttrVO;
import com.atguigu.gulimall.product.vo.AttrValueVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 商品属性
 *
 * @author ls
 * @email 1725248162@qq.com
 * @date 2021-06-30 10:20:10
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {

    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    @PostMapping("/update/{spuId}")
    public R updateProductAttrValue(@PathVariable Long spuId,
                                    @RequestBody List<AttrValueVo> attrValueVos) {
        productAttrValueService.updateProductAttrValue(spuId, attrValueVos);
        return R.ok();
    }

    @GetMapping("/base/listforspu/{spuId}")
    public R listProductAttrValue(@PathVariable Long spuId) {
        List<ProductAttrValueEntity> list = productAttrValueService.list(Wrappers.<ProductAttrValueEntity>lambdaQuery()
                .eq(ProductAttrValueEntity::getSpuId, spuId));
        return R.ok().put("data", list);
    }

    /**
     * product/attr/base/list/1 基本属性
     * product/attr/sale/list/1 销售属性
     * 分页查询页面所需属性
     * @return
     */
    @GetMapping("/{type}/list/{catelogId}")
    public R listAttr(@PathVariable("type") String type,
                      @PathVariable("catelogId") Long catelogId,
                      @RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryAttrPage(type,catelogId, params);

        return R.ok().put("page", page);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
        AttrResVo resVo = attrService.getAttrDetailById(attrId);

        return R.ok().put("attr", resVo);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody AttrVO attrVO){
		attrService.saveAttr(attrVO);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVO attrVO){
		attrService.updateAttrDetailById(attrVO);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
